package gui.nodes;

import gui.AbstractViewPane;
import gui.AnneeEtudeUIConsulter;
import gui.CongeUIConsulter;
import gui.FormationUIConsulter;
import gui.MainController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import utility.Record;
import business.CongeBL;
import business.FormationBL;

public class CongeNode extends AbstractLeaf {

	int congeId;
	private int anneeEtudeId;
	String congeName;
	DefaultMutableTreeNode congeNode;
	AbstractNode parentNode;
	
	public CongeNode(CongeRoot parentNode, Record recordFormation, int aeId) {
		anneeEtudeId = aeId;
		this.parentNode = parentNode;
		this.congeId = recordFormation.getId();
		this.congeName = recordFormation.getLibelle();
	}

	@Override
	public JPopupMenu makePopup() {
		return new Popup();
	}

	@Override
	public String getNodeName() {
		return congeName;
	}

	public void loadPrimaryViewPane() {
		AbstractViewPane newViewPane = new CongeUIConsulter(parentNode,
				AbstractViewPane.ViewMode.CONSULTATION, congeId, anneeEtudeId);
		MainController.getInstance().setViewPane(newViewPane);
	}

	public void loadModifierViewPane() {
		AbstractViewPane newViewPane = new CongeUIConsulter(parentNode,
				AbstractViewPane.ViewMode.MODIFICATION, congeId, anneeEtudeId);
		MainController.getInstance().setViewPane(newViewPane);
	}

	public void loadSupprimerViewPane() {
		if (supprimer() == JOptionPane.YES_OPTION) {
			CongeBL congeBL = new CongeBL(congeId);
			congeBL.supprimerConge();
		}
		((AbstractNode)parent).refresh();
	}

	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem consulter = new JMenuItem("Consulter les informations");
		JMenuItem modifier = new JMenuItem("Modifier le congé");
		JMenuItem supprimer = new JMenuItem("Supprimer le congé");

		public Popup() {
			consulter.addActionListener(this);
			modifier.addActionListener(this);
			supprimer.addActionListener(this);
			add(consulter);
			add(modifier);
			add(supprimer);
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == consulter) {
				loadPrimaryViewPane();
			}
			if (e.getSource() == modifier) {
				loadModifierViewPane();
			}
			if (e.getSource() == supprimer) {
				loadSupprimerViewPane();
			}

		}

	}

	public int supprimer() {
		return JOptionPane.showConfirmDialog(
			    null,
			    "Êtes vous sûr de vouloir supprimer le congé\n"+
			    //ensBL.getName()+" "+ensBL.getPrenom()+
			    " ?",
			    "Avertissement",
			    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		
	}

}
